package com.codeoss.framework.view.pagebean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.clientscript.JsWindow;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.IFormFieldExpression;
import com.codeoss.framework.view.component.IVarName;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.service.IUReport2ComponentService;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.ComponentDataUtil;
import com.codeoss.framework.view.util.PageBeanUtil;

@Service("uReport2PageBean")
public class UReport2PageBean extends BasePageBean {
	public static final String KEY_SEARCH_FIELD_LIST = "key_search_field_list";

	public static final String KEY_UREPORT2 = "key_ureport2";
	
	@Autowired
	private IVariableService variableService;
	@Autowired
	private IUReport2ComponentService uReport2ComponentService;
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Autowired
	private ComponentDataUtil componentDataUtil;
	public void beforeInit() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		if (page.isInit()) {
			return;
		}
		
		List<IDataBinding> searchDataBindingList =  new ArrayList<IDataBinding>();
		pageBeanUtil.getFormFieldList(page, searchDataBindingList);
		page.setAttribute(KEY_SEARCH_FIELD_LIST, searchDataBindingList);

		pageBeanUtil.getComponent(yunContext, page, "ureport2");
		com.codeoss.framework.view.component.report.UReport2 ureport2Component = (com.codeoss.framework.view.component.report.UReport2) yunContext
				.getAttribute("__component__");
		page.setAttribute(KEY_UREPORT2, ureport2Component);

		page.setInit(true);
	}
	
	public void init() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		List<IDataBinding> searchDataBindingList=page.getAttribute(KEY_SEARCH_FIELD_LIST);
		if(searchDataBindingList.size()>0) {
			for(int i=0;i<searchDataBindingList.size();i++) {
				IDataBinding dataBinding = searchDataBindingList.get(i);
				if(dataBinding instanceof IFormFieldExpression) {
					Object  defaultValue=componentDataUtil.getDefaultValue(yunContext, page, (IFormFieldExpression)dataBinding);
					variableService.setVariableValue(yunContext,((IVarName)dataBinding).getVarName(), defaultValue);
				}else {
					variableService.setVariableValue(yunContext,((IVarName)dataBinding).getVarName(), null);
				}
			}
			pageBeanUtil.generateFormInfo(searchDataBindingList);
		}
	}
	
	public void dataSearch() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		com.codeoss.framework.view.component.report.UReport2 ureport2Component = (com.codeoss.framework.view.component.report.UReport2) page.getAttribute(KEY_UREPORT2);
		
		Map<String,Object> paraMap=new HashMap<String,Object>();
		List<IDataBinding> searchDataBindingList =  page.getAttribute(KEY_SEARCH_FIELD_LIST);
		for (int j = 0; j < searchDataBindingList.size(); j++) {
			IDataBinding dataBinding = searchDataBindingList.get(j);
			Object value = variableService.getVariableValue(yunContext, ((IVarName) dataBinding).getVarName());
			paraMap.put(dataBinding.getDataField(), value);
		}
		uReport2ComponentService.dataQuery(yunContext, ureport2Component, paraMap);
	}
	
	public void dataExport(String format,String reportName) {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		com.codeoss.framework.view.component.report.UReport2 ureport2Component = (com.codeoss.framework.view.component.report.UReport2) page.getAttribute(KEY_UREPORT2);
		
		Map<String,Object> paraMap=new HashMap<String,Object>();
		List<IDataBinding> searchDataBindingList = page.getAttribute(KEY_SEARCH_FIELD_LIST);
		for (int j = 0; j < searchDataBindingList.size(); j++) {
			IDataBinding dataBinding = searchDataBindingList.get(j);
			Object value = variableService.getVariableValue(yunContext, ((IVarName) dataBinding).getVarName());
			paraMap.put(dataBinding.getDataField(), value);
		}
		String filename=uReport2ComponentService.dataExport(yunContext, ureport2Component, paraMap,format);
		JsWindow jsWindow=JsWindow.open("file/download?filename="+filename+"&displayName="+reportName+"."+format+"&X-Token="+yunContext.getToken());
		yunContext.addClientScript(jsWindow);
		yunContext.setRenderCompleted(true);
	}
}